{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# 3.8 Nonlinear minimization problems\n",
    "We consider problems of the form\n",
    "\n",
    "$$\\text{find } u \\in V \\text{ s.t. } E(u) \\leq E(v) \\quad \\forall~  v \\in V.$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "import netgen.gui\n",
    "%gui tk\n",
    "from ngsolve import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Scalar minimization problems\n",
    "As a first example we take $V = H^1_0$ and \n",
    "\n",
    "$$\n",
    "E(u) = \\int_{\\Omega} \\vert \\nabla u \\vert^2 + u^4 - fu ~dx.\n",
    "$$\n",
    "\n",
    "The minimization is equivalent to solving the nonlinear PDE:\n",
    "\n",
    "$$\n",
    " - \\Delta u + 4 u^3 = f \\text{ in } \\Omega\n",
    "$$\n",
    "\n",
    "We solve the PDE with a Newton iteration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "from netgen.geom2d import unit_square\n",
    "\n",
    "mesh = Mesh (unit_square.GenerateMesh(maxh=0.2))\n",
    "V = H1(mesh, order=4, dirichlet=[1,2,3,4])\n",
    "u = V.TrialFunction()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "To solve the problem we use the `SymbolicEnergy` integrator. Based on the symbolic description of the energy functional, it is able to \n",
    "\n",
    "* evaluate the energy functional (`Energy`)\n",
    "\n",
    "$$E(u) \\qquad (E:V \\to \\mathbb{R})$$\n",
    "\n",
    "* compute the Gateau derivative for a given $u$ (`Apply`):\n",
    "\n",
    "$$A(u)(v) = E'(u)(v) \\qquad (A(u): V \\to \\mathbb{R})$$\n",
    "\n",
    "* compute the second derivative (`AssembleLinearization`)\n",
    "\n",
    "$$(\\delta A)(w)(u,v) \\qquad (\\delta A(w): V\\times V \\to \\mathbb{R})$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "a = BilinearForm (V, symmetric=False)\n",
    "a += SymbolicEnergy ( grad(u)*grad(u) + u**4-u )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "source": [
    "Equivalent to:\n",
    "```\n",
    "a += SymbolicBFI( 2 * grad(u) * grad(v) + 4*u*u*u*v - 1 * v)\n",
    "``` \n",
    "(which has the same form as the problems in [the nonlinear example](../unit-3.7-nonlinear/nonlinear.ipynb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We recall the Newton iteration (cf. [unit-3.7](../unit-3.7-nonlinear/nonlinear.ipynb) ) we make the loop:\n",
    "\n",
    "- Given an initial guess $u^0$\n",
    "- loop over $i=0,..$ until convergence:\n",
    "  - Compute linearization: $A u^i + \\delta A(u^i) \\Delta u^{i} = 0 $:\n",
    "    - $f^i = A u^i$ \n",
    "    - $B^i = \\delta A(u^i)$ \n",
    "    - Solve $B^i \\Delta u^i = -f^i$\n",
    "  - Update $u^{i+1} = u^i + \\Delta u^{i}$\n",
    "  - Evaluate stopping criteria\n",
    "- Evaluate $E(u^{i+1})$\n",
    "\n",
    "As a stopping criteria we take $\\langle A u^i,\\Delta u^i \\rangle = \\langle A u^i, A u^i \\rangle_{(B^i)^{-1}}< \\varepsilon$.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "def SolveNonlinearMinProblem(a,gfu,tol=1e-13,maxits=25):\n",
    "    res = gfu.vec.CreateVector()\n",
    "    du  = gfu.vec.CreateVector()\n",
    "\n",
    "    for it in range(maxits):\n",
    "        print (\"Newton iteration {:3}\".format(it),end=\"\")\n",
    "        print (\"energy = {:16}\".format(a.Energy(gfu.vec)),end=\"\")\n",
    "    \n",
    "        #solve linearized problem:\n",
    "        a.Apply (gfu.vec, res)\n",
    "        a.AssembleLinearization (gfu.vec)\n",
    "        inv = a.mat.Inverse(V.FreeDofs())\n",
    "        du.data = inv * res\n",
    "    \n",
    "        #update iteration\n",
    "        gfu.vec.data -= du\n",
    "\n",
    "        #stopping criteria\n",
    "        stopcritval = sqrt(abs(InnerProduct(du,res)))\n",
    "        print (\"<A u\",it,\", A u\",it,\">_{-1}^0.5 = \", stopcritval)\n",
    "        if stopcritval < tol:\n",
    "            break\n",
    "        Redraw(blocking=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Newton iteration   0energy =              0.0<A u 0 , A u 0 >_{-1}^0.5 =  0.1325595815712792\n",
      "Newton iteration   1energy = -0.008785677986915054<A u 1 , A u 1 >_{-1}^0.5 =  1.1107597333627496e-05\n",
      "Newton iteration   2energy = -0.008785678048604416<A u 2 , A u 2 >_{-1}^0.5 =  2.807506601278265e-13\n",
      "Newton iteration   3energy = -0.008785678048604414<A u 3 , A u 3 >_{-1}^0.5 =  3.069514454612314e-17\n",
      "energy =  -0.008785678048604414\n"
     ]
    }
   ],
   "source": [
    "gfu = GridFunction (V)\n",
    "gfu.vec[:] = 0\n",
    "Draw(gfu,mesh,\"u\")\n",
    "\n",
    "SolveNonlinearMinProblem(a,gfu)\n",
    "\n",
    "print (\"energy = \", a.Energy(gfu.vec))\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Nonlinear elasticity\n",
    "\n",
    "We consider a beam which is fixed on one side and is subject to gravity only. We assume a Neo-Hookean hyperelastic material. The model is a nonlinear minimization problem with \n",
    "\n",
    "$$\n",
    "  E(v) := \\int_{\\Omega} \\frac{\\mu}{2} ( \\operatorname{tr}(F^T F-I)+\\frac{2 \\mu}{\\lambda} \\operatorname{det}(F^T F)^{\\frac{\\lambda}{2\\mu}} - 1) - \\gamma ~ (f,v) ~~ dx\n",
    "$$\n",
    "\n",
    "where $\\mu$ and $\\lambda$ are the Lamé parameters and $F = I + D v$ where $v: \\Omega \\to \\mathbb{R}^2$ is the sought for displacement."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "import netgen.geom2d as geom2d\n",
    "from ngsolve import *\n",
    "\n",
    "geo = geom2d.SplineGeometry()\n",
    "pnums = [ geo.AddPoint (x,y,maxh=0.01) for x,y in [(0,0), (1,0), (1,0.1), (0,0.1)] ]\n",
    "for p1,p2,bc in [(0,1,\"bot\"), (1,2,\"right\"), (2,3,\"top\"), (3,0,\"left\")]:\n",
    "     geo.Append([\"line\", pnums[p1], pnums[p2]], bc=bc)\n",
    "mesh = Mesh(geo.GenerateMesh(maxh=0.05))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "# E module and poisson number:\n",
    "E, nu = 210, 0.2\n",
    "# Lamé constants:\n",
    "mu  = E / 2 / (1+nu)\n",
    "lam = E * nu / ((1+nu)*(1-2*nu))\n",
    "\n",
    "V = H1(mesh, order=2, dirichlet=\"left\", dim=mesh.dim)\n",
    "u  = V.TrialFunction()\n",
    "\n",
    "#gravity:\n",
    "force = CoefficientFunction( (0,-1) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "def Pow(a, b):\n",
    "    return exp (log(a)*b)\n",
    "    \n",
    "def NeoHook (C):\n",
    "    return 0.5 * mu * (Trace(C-I) + 2*mu/lam * Pow(Det(C), -lam/2/mu) - 1)\n",
    "\n",
    "I = Id(mesh.dim)\n",
    "F = I + u.Deriv()   # attention: row .. component, col .. derivative\n",
    "C = F * F.trans  \n",
    "\n",
    "factor = Parameter(1.0)\n",
    "\n",
    "a = BilinearForm(V, symmetric=False)\n",
    "a += SymbolicEnergy(  NeoHook (C).Compile() )\n",
    "a += SymbolicEnergy(  (-factor * InnerProduct(force,u) ).Compile() )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "We want to solve the minimization problem for $\\gamma = 5$. Due to the high nonlinearity in the problem, the Newton iteration will not convergence with any initial guess. We approach the case $\\gamma = 5$ by solving problems with $\\gamma = i/10$ for $i=1,..,50$ and taking the solution of the previous problem as an initial guess."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "loadstep 0\n",
      "Newton iteration   0energy = 8.749999999999996<A u 0 , A u 0 >_{-1}^0.5 =  0.016678457857220372\n",
      "Newton iteration   1energy = 8.750132589925284<A u 1 , A u 1 >_{-1}^0.5 =  0.023333465280071634\n",
      "Newton iteration   2energy = 8.749861149642193<A u 2 , A u 2 >_{-1}^0.5 =  0.000102324871854834\n",
      "Newton iteration   3energy = 8.749861144406996<A u 3 , A u 3 >_{-1}^0.5 =  5.1656126548647754e-08\n",
      "Newton iteration   4energy = 8.749861144407008<A u 4 , A u 4 >_{-1}^0.5 =  2.7534833076896285e-13\n",
      "Newton iteration   5energy = 8.749861144407008<A u 5 , A u 5 >_{-1}^0.5 =  7.942625526684423e-16\n",
      "loadstep 1\n",
      "Newton iteration   0energy = 8.749583890792827<A u 0 , A u 0 >_{-1}^0.5 =  0.016596122310545317\n",
      "Newton iteration   1energy = 8.749710095753294<A u 1 , A u 1 >_{-1}^0.5 =  0.022958368235712307\n",
      "Newton iteration   2energy = 8.74944729855201<A u 2 , A u 2 >_{-1}^0.5 =  0.00014216311244477864\n",
      "Newton iteration   3energy = 8.749447288552698<A u 3 , A u 3 >_{-1}^0.5 =  2.3257814734367656e-06\n",
      "Newton iteration   4energy = 8.749447288549993<A u 4 , A u 4 >_{-1}^0.5 =  6.330184125593997e-10\n",
      "Newton iteration   5energy = 8.749447288549998<A u 5 , A u 5 >_{-1}^0.5 =  1.3145920159023592e-15\n",
      "loadstep 2\n",
      "Newton iteration   0energy = 8.748898146779494<A u 0 , A u 0 >_{-1}^0.5 =  0.01635691851768748\n",
      "Newton iteration   1energy = 8.74900526217155<A u 1 , A u 1 >_{-1}^0.5 =  0.02189268120656001\n",
      "Newton iteration   2energy =  8.7487662601439<A u 2 , A u 2 >_{-1}^0.5 =  0.00020822868315535764\n",
      "Newton iteration   3energy = 8.748766238787264<A u 3 , A u 3 >_{-1}^0.5 =  4.891163159250807e-06\n",
      "Newton iteration   4energy = 8.748766238775303<A u 4 , A u 4 >_{-1}^0.5 =  2.6654685723972467e-09\n",
      "Newton iteration   5energy = 8.748766238775298<A u 5 , A u 5 >_{-1}^0.5 =  1.9181997773232907e-15\n",
      "loadstep 3\n",
      "Newton iteration   0energy = 8.747955305248166<A u 0 , A u 0 >_{-1}^0.5 =  0.015981995303982335\n",
      "Newton iteration   1energy = 8.748035431072346<A u 1 , A u 1 >_{-1}^0.5 =  0.020292141712791756\n",
      "Newton iteration   2energy = 8.747830050915914<A u 2 , A u 2 >_{-1}^0.5 =  0.00026380941255572184\n",
      "Newton iteration   3energy = 8.74783001660982<A u 3 , A u 3 >_{-1}^0.5 =  7.247527319852685e-06\n",
      "Newton iteration   4energy = 8.747830016583555<A u 4 , A u 4 >_{-1}^0.5 =  4.744276205791727e-09\n",
      "Newton iteration   5energy = 8.747830016583547<A u 5 , A u 5 >_{-1}^0.5 =  3.426563064439674e-15\n",
      "loadstep 4\n",
      "Newton iteration   0energy = 8.74677103329084<A u 0 , A u 0 >_{-1}^0.5 =  0.015500978426366845\n",
      "Newton iteration   1energy = 8.746821875264395<A u 1 , A u 1 >_{-1}^0.5 =  0.018359632535331443\n",
      "Newton iteration   2energy = 8.74665370335822<A u 2 , A u 2 >_{-1}^0.5 =  0.0002990660120298062\n",
      "Newton iteration   3energy = 8.746653659164602<A u 3 , A u 3 >_{-1}^0.5 =  8.720462079331242e-06\n",
      "Newton iteration   4energy = 8.74665365912657<A u 4 , A u 4 >_{-1}^0.5 =  5.242979774834111e-09\n",
      "Newton iteration   5energy = 8.746653659126574<A u 5 , A u 5 >_{-1}^0.5 =  3.97720967385856e-15\n",
      "loadstep 5\n",
      "Newton iteration   0energy = 8.745362746174253<A u 0 , A u 0 >_{-1}^0.5 =  0.014946136334759453\n",
      "Newton iteration   1energy = 8.745386494953838<A u 1 , A u 1 >_{-1}^0.5 =  0.016292705582918978\n",
      "Newton iteration   2energy = 8.745254015487305<A u 2 , A u 2 >_{-1}^0.5 =  0.0003132045535723567\n",
      "Newton iteration   3energy = 8.745253966886729<A u 3 , A u 3 >_{-1}^0.5 =  9.042883675557174e-06\n",
      "Newton iteration   4energy = 8.745253966845835<A u 4 , A u 4 >_{-1}^0.5 =  4.2168701438486075e-09\n",
      "Newton iteration   5energy = 8.745253966845846<A u 5 , A u 5 >_{-1}^0.5 =  3.598406255743586e-15\n",
      "loadstep 6\n",
      "Newton iteration   0energy = 8.74374841975918<A u 0 , A u 0 >_{-1}^0.5 =  0.014347613422509365\n",
      "Newton iteration   1energy = 8.743749808685779<A u 1 , A u 1 >_{-1}^0.5 =  0.014248822606138294\n",
      "Newton iteration   2energy = 8.743648449537007<A u 2 , A u 2 >_{-1}^0.5 =  0.00030934466833337424\n",
      "Newton iteration   3energy = 8.743648402011962<A u 3 , A u 3 >_{-1}^0.5 =  8.384742090565533e-06\n",
      "Newton iteration   4energy = 8.743648401976813<A u 4 , A u 4 >_{-1}^0.5 =  2.693807895239245e-09\n",
      "Newton iteration   5energy = 8.743648401976808<A u 5 , A u 5 >_{-1}^0.5 =  3.709720057706471e-15\n",
      "loadstep 7\n",
      "Newton iteration   0energy = 8.74194570825174<A u 0 , A u 0 >_{-1}^0.5 =  0.0137306209358126\n",
      "Newton iteration   1energy = 8.741930247461754<A u 1 , A u 1 >_{-1}^0.5 =  0.012333315340035126\n",
      "Newton iteration   2energy = 8.741854283366798<A u 2 , A u 2 >_{-1}^0.5 =  0.0002921290506727813\n",
      "Newton iteration   3energy = 8.741854240900244<A u 3 , A u 3 >_{-1}^0.5 =  7.129861729080879e-06\n",
      "Newton iteration   4energy = 8.741854240874824<A u 4 , A u 4 >_{-1}^0.5 =  1.443342960710022e-09\n",
      "Newton iteration   5energy = 8.74185424087483<A u 5 , A u 5 >_{-1}^0.5 =  4.016975656450898e-15\n",
      "loadstep 8\n",
      "Newton iteration   0energy = 8.739971370076443<A u 0 , A u 0 >_{-1}^0.5 =  0.013114473688276701\n",
      "Newton iteration   1energy = 8.739944202308775<A u 1 , A u 1 >_{-1}^0.5 =  0.010603784256638427\n",
      "Newton iteration   2energy = 8.739888031385908<A u 2 , A u 2 >_{-1}^0.5 =  0.0002663171865137599\n",
      "Newton iteration   3energy = 8.739887996038714<A u 3 , A u 3 >_{-1}^0.5 =  5.662593370338205e-06\n",
      "Newton iteration   4energy = 8.739887996022675<A u 4 , A u 4 >_{-1}^0.5 =  6.719431899287913e-10\n",
      "Newton iteration   5energy = 8.73988799602267<A u 5 , A u 5 >_{-1}^0.5 =  4.3773856090675734e-15\n",
      "loadstep 9\n",
      "Newton iteration   0energy = 8.73784094712138<A u 0 , A u 0 >_{-1}^0.5 =  0.01251287296514784\n",
      "Newton iteration   1energy = 8.737806316551259<A u 1 , A u 1 >_{-1}^0.5 =  0.009081886577209623\n",
      "Newton iteration   2energy = 8.737765099398299<A u 2 , A u 2 >_{-1}^0.5 =  0.00023603860491985037\n",
      "Newton iteration   3energy = 8.737765071600855<A u 3 , A u 3 >_{-1}^0.5 =  4.258372559407323e-06\n",
      "Newton iteration   4energy = 8.73776507159178<A u 4 , A u 4 >_{-1}^0.5 =  2.778505250314751e-10\n",
      "Newton iteration   5energy = 8.73776507159178<A u 5 , A u 5 >_{-1}^0.5 =  6.056468230296881e-15\n",
      "loadstep 10\n",
      "Newton iteration   0energy = 8.735568627127268<A u 0 , A u 0 >_{-1}^0.5 =  0.011934806211101961\n",
      "Newton iteration   1energy = 8.735529764422171<A u 1 , A u 1 >_{-1}^0.5 =  0.0077659135356494555\n",
      "Newton iteration   2energy = 8.73549961771559<A u 2 , A u 2 >_{-1}^0.5 =  0.00020449613889987533\n",
      "Newton iteration   3energy = 8.735499596834673<A u 3 , A u 3 >_{-1}^0.5 =  3.0647929288143972e-06\n",
      "Newton iteration   4energy = 8.735499596829976<A u 4 , A u 4 >_{-1}^0.5 =  1.0315667610823363e-10\n",
      "Newton iteration   5energy = 8.73549959682998<A u 5 , A u 5 >_{-1}^0.5 =  4.943459155768916e-15\n",
      "loadstep 11\n",
      "Newton iteration   0energy = 8.733167226220827<A u 0 , A u 0 >_{-1}^0.5 =  0.011385614969521708\n",
      "Newton iteration   1energy = 8.73312644913484<A u 1 , A u 1 >_{-1}^0.5 =  0.006641005193965611\n",
      "Newton iteration   2energy = 8.733104397426635<A u 2 , A u 2 >_{-1}^0.5 =  0.00017393963828985667\n",
      "Newton iteration   3energy = 8.73310438231158<A u 3 , A u 3 >_{-1}^0.5 =  2.1292807624511996e-06\n",
      "Newton iteration   4energy = 8.733104382309314<A u 4 , A u 4 >_{-1}^0.5 =  3.432001970407436e-11\n",
      "Newton iteration   5energy = 8.733104382309314<A u 5 , A u 5 >_{-1}^0.5 =  6.309315793552211e-15\n",
      "loadstep 12\n",
      "Newton iteration   0energy = 8.730648243500866<A u 0 , A u 0 >_{-1}^0.5 =  0.010867985330305738\n",
      "Newton iteration   1energy = 8.730607135592724<A u 1 , A u 1 >_{-1}^0.5 =  0.005686263271293424\n",
      "Newton iteration   2energy = 8.73059096457382<A u 2 , A u 2 >_{-1}^0.5 =  0.00014577993437518897\n",
      "Newton iteration   3energy = 8.73059095395286<A u 3 , A u 3 >_{-1}^0.5 =  1.4383390443423343e-06\n",
      "Newton iteration   4energy = 8.73059095395182<A u 4 , A u 4 >_{-1}^0.5 =  9.985478077309379e-12\n",
      "Newton iteration   5energy = 8.730590953951822<A u 5 , A u 5 >_{-1}^0.5 =  6.304937632385649e-15\n",
      "loadstep 13\n",
      "Newton iteration   0energy = 8.728021954318086<A u 0 , A u 0 >_{-1}^0.5 =  0.01038276202251739\n",
      "Newton iteration   1energy = 8.727981547860873<A u 1 , A u 1 >_{-1}^0.5 =  0.004879172690876309\n",
      "Newton iteration   2energy = 8.727969638945583<A u 2 , A u 2 >_{-1}^0.5 =  0.00012076034968403019\n",
      "Newton iteration   3energy = 8.727969631655753<A u 3 , A u 3 >_{-1}^0.5 =  9.504870926466129e-07\n",
      "Newton iteration   4energy = 8.727969631655302<A u 4 , A u 4 >_{-1}^0.5 =  2.3259742706803135e-12\n",
      "Newton iteration   5energy = 8.727969631655306<A u 5 , A u 5 >_{-1}^0.5 =  7.021360363946004e-15\n",
      "loadstep 14\n",
      "Newton iteration   0energy = 8.72529752061992<A u 0 , A u 0 >_{-1}^0.5 =  0.00992957077965983\n",
      "Newton iteration   1energy = 8.725258452639936<A u 1 , A u 1 >_{-1}^0.5 =  0.004198069592467644\n",
      "Newton iteration   2energy = 8.725249634804557<A u 2 , A u 2 >_{-1}^0.5 =  9.913700636600242e-05\n",
      "Newton iteration   3energy = 8.725249629890909<A u 3 , A u 3 >_{-1}^0.5 =  6.177005897210002e-07\n",
      "Newton iteration   4energy = 8.725249629890715<A u 4 , A u 4 >_{-1}^0.5 =  2.7075801825504915e-13\n",
      "Newton iteration   5energy = 8.72524962989072<A u 5 , A u 5 >_{-1}^0.5 =  6.4867867334457204e-15\n",
      "loadstep 15\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Newton iteration   0energy = 8.722483105125077<A u 0 , A u 0 >_{-1}^0.5 =  0.009507271533554363\n",
      "Newton iteration   1energy = 8.722445738804593<A u 1 , A u 1 >_{-1}^0.5 =  0.003623338365125314\n",
      "Newton iteration   2energy = 8.722439169043259<A u 2 , A u 2 >_{-1}^0.5 =  8.084105831464236e-05\n",
      "Newton iteration   3energy = 8.722439165775617<A u 3 , A u 3 >_{-1}^0.5 =  3.9657874014284555e-07\n",
      "Newton iteration   4energy = 8.72243916577554<A u 4 , A u 4 >_{-1}^0.5 =  1.5322265915943703e-13\n",
      "Newton iteration   5energy = 8.72243916577554<A u 5 , A u 5 >_{-1}^0.5 =  7.020924862732914e-15\n",
      "loadstep 16\n",
      "Newton iteration   0energy = 8.719585981725038<A u 0 , A u 0 >_{-1}^0.5 =  0.009114276766163665\n",
      "Newton iteration   1energy = 8.719550495831808<A u 1 , A u 1 >_{-1}^0.5 =  0.003137848417803922\n",
      "Newton iteration   2energy = 8.719545568051617<A u 2 , A u 2 >_{-1}^0.5 =  6.561021540824207e-05\n",
      "Newton iteration   3energy = 8.719545565899164<A u 3 , A u 3 >_{-1}^0.5 =  2.525155744874241e-07\n",
      "Newton iteration   4energy = 8.719545565899123<A u 4 , A u 4 >_{-1}^0.5 =  1.4980696449755864e-13\n",
      "Newton iteration   5energy = 8.719545565899129<A u 5 , A u 5 >_{-1}^0.5 =  7.025195703399348e-15\n",
      "loadstep 17\n",
      "Newton iteration   0energy = 8.716612638121687<A u 0 , A u 0 >_{-1}^0.5 =  0.008748768597255036\n",
      "Newton iteration   1energy = 8.716579090586363<A u 1 , A u 1 >_{-1}^0.5 =  0.0027269718667353913\n",
      "Newton iteration   2energy = 8.716575368455187<A u 2 , A u 2 >_{-1}^0.5 =  5.30864063971943e-05\n",
      "Newton iteration   3energy = 8.716575367045994<A u 3 , A u 3 >_{-1}^0.5 =  1.5998581937951101e-07\n",
      "Newton iteration   4energy = 8.716575367045984<A u 4 , A u 4 >_{-1}^0.5 =  9.24264467888606e-14\n",
      "loadstep 18\n",
      "Newton iteration   0energy = 8.713568868939605<A u 0 , A u 0 >_{-1}^0.5 =  0.008408842703937033\n",
      "Newton iteration   1energy = 8.713537240942847<A u 1 , A u 1 >_{-1}^0.5 =  0.0023783933491091343\n",
      "Newton iteration   2energy =  8.7135344093602<A u 2 , A u 2 >_{-1}^0.5 =  4.288234198914801e-05\n",
      "Newton iteration   3energy = 8.713534408440674<A u 3 , A u 3 >_{-1}^0.5 =  1.0113452182494698e-07\n",
      "Newton iteration   4energy = 8.713534408440665<A u 4 , A u 4 >_{-1}^0.5 =  4.866020392496476e-14\n",
      "loadstep 19\n",
      "Newton iteration   0energy = 8.71045985887359<A u 0 , A u 0 >_{-1}^0.5 =  0.008092600787386375\n",
      "Newton iteration   1energy = 8.710430084840077<A u 1 , A u 1 >_{-1}^0.5 =  0.0020818348293488013\n",
      "Newton iteration   2energy = 8.710427915258844<A u 2 , A u 2 >_{-1}^0.5 =  3.462274032326428e-05\n",
      "Newton iteration   3energy = 8.710427914659423<A u 3 , A u 3 >_{-1}^0.5 =  6.393209442938853e-08\n",
      "Newton iteration   4energy = 8.710427914659427<A u 4 , A u 4 >_{-1}^0.5 =  2.4472323028745738e-14\n",
      "loadstep 20\n",
      "Newton iteration   0energy = 8.707290256175286<A u 0 , A u 0 >_{-1}^0.5 =  0.007798207601916305\n",
      "Newton iteration   1energy = 8.707262243846786<A u 1 , A u 1 >_{-1}^0.5 =  0.0018287624748981241\n",
      "Newton iteration   2energy = 8.70726056963296<A u 2 , A u 2 >_{-1}^0.5 =  2.7966782719369756e-05\n",
      "Newton iteration   3energy = 8.707260569241857<A u 3 , A u 3 >_{-1}^0.5 =  4.048825692057199e-08\n",
      "Newton iteration   4energy = 8.707260569241852<A u 4 , A u 4 >_{-1}^0.5 =  1.2768784682907216e-14\n",
      "loadstep 21\n",
      "Newton iteration   0energy = 8.704064237168412<A u 0 , A u 0 >_{-1}^0.5 =  0.007523924008749803\n",
      "Newton iteration   1energy = 8.704037880818536<A u 1 , A u 1 >_{-1}^0.5 =  0.0016121092918853037\n",
      "Newton iteration   2energy = 8.704036579777165<A u 2 , A u 2 >_{-1}^0.5 =  2.2617802715322334e-05\n",
      "Newton iteration   3energy = 8.70403657952137<A u 3 , A u 3 >_{-1}^0.5 =  2.5725058090116162e-08\n",
      "Newton iteration   4energy = 8.704036579521365<A u 4 , A u 4 >_{-1}^0.5 =  8.839028230895453e-15\n",
      "loadstep 22\n",
      "Newton iteration   0energy = 8.700785562650578<A u 0 , A u 0 >_{-1}^0.5 =  0.0072681240911013885\n",
      "Newton iteration   1energy = 8.700760751620374<A u 1 , A u 1 >_{-1}^0.5 =  0.0014260281489234519\n",
      "Newton iteration   2energy = 8.700759733595584<A u 2 , A u 2 >_{-1}^0.5 =  1.8325012042621618e-05\n",
      "Newton iteration   3energy = 8.700759733427669<A u 3 , A u 3 >_{-1}^0.5 =  1.6416821349247635e-08\n",
      "Newton iteration   4energy = 8.70075973342767<A u 4 , A u 4 >_{-1}^0.5 =  7.0644037247864004e-15\n",
      "loadstep 23\n",
      "Newton iteration   0energy = 8.697457627081807<A u 0 , A u 0 >_{-1}^0.5 =  0.007029301882349006\n",
      "Newton iteration   1energy = 8.69743425115637<A u 1 , A u 1 >_{-1}^0.5 =  0.0012656794747770324\n",
      "Newton iteration   2energy = 8.69743344921061<A u 2 , A u 2 >_{-1}^0.5 =  1.4880750185375254e-05\n",
      "Newton iteration   3energy = 8.697433449099881<A u 3 , A u 3 >_{-1}^0.5 =  1.0531732189483211e-08\n",
      "Newton iteration   4energy = 8.697433449099886<A u 4 , A u 4 >_{-1}^0.5 =  7.324873708442945e-15\n",
      "loadstep 24\n",
      "Newton iteration   0energy = 8.694083501431793<A u 0 , A u 0 >_{-1}^0.5 =  0.006806071495723261\n",
      "Newton iteration   1energy = 8.694061454104311<A u 1 , A u 1 >_{-1}^0.5 =  0.0011270526725460628\n",
      "Newton iteration   2energy = 8.694060818220638<A u 2 , A u 2 >_{-1}^0.5 =  1.2115589040157976e-05\n",
      "Newton iteration   3energy = 8.69406081814725<A u 3 , A u 3 >_{-1}^0.5 =  6.7961929805900055e-09\n",
      "Newton iteration   4energy = 8.694060818147245<A u 4 , A u 4 >_{-1}^0.5 =  7.34815895816638e-15\n",
      "loadstep 25\n",
      "Newton iteration   0energy = 8.69066597049359<A u 0 , A u 0 >_{-1}^0.5 =  0.006597163213874111\n",
      "Newton iteration   1energy = 8.690645150830838<A u 1 , A u 1 >_{-1}^0.5 =  0.0010068179295690766\n",
      "Newton iteration   2energy = 8.690644643396258<A u 2 , A u 2 >_{-1}^0.5 =  9.892734819757748e-06\n",
      "Newton iteration   3energy = 8.690644643347325<A u 3 , A u 3 >_{-1}^0.5 =  4.413530878878279e-09\n",
      "Newton iteration   4energy = 8.690644643347321<A u 4 , A u 4 >_{-1}^0.5 =  8.077944067857594e-15\n",
      "loadstep 26\n",
      "Newton iteration   0energy = 8.687207565391779<A u 0 , A u 0 >_{-1}^0.5 =  0.006401417245850291\n",
      "Newton iteration   1energy = 8.687187878984766<A u 1 , A u 1 >_{-1}^0.5 =  0.0009022042935673603\n",
      "Newton iteration   2energy = 8.687187471534324<A u 2 , A u 2 >_{-1}^0.5 =  8.102543734498749e-06\n",
      "Newton iteration   3energy = 8.687187471501492<A u 3 , A u 3 >_{-1}^0.5 =  2.8853474185662717e-09\n",
      "Newton iteration   4energy = 8.687187471501497<A u 4 , A u 4 >_{-1}^0.5 =  7.88933517416324e-15\n",
      "loadstep 27\n",
      "Newton iteration   0energy = 8.683710591929625<A u 0 , A u 0 >_{-1}^0.5 =  0.006217776276937307\n",
      "Newton iteration   1energy = 8.683691951255541<A u 1 , A u 1 >_{-1}^0.5 =  0.000810899873211364\n",
      "Newton iteration   2energy =  8.6836916221124<A u 2 , A u 2 >_{-1}^0.5 =  6.6575598591021325e-06\n",
      "Newton iteration   3energy = 8.68369162209024<A u 3 , A u 3 >_{-1}^0.5 =  1.899274794240032e-09\n",
      "Newton iteration   4energy = 8.683691622090233<A u 4 , A u 4 >_{-1}^0.5 =  7.480573029729903e-15\n",
      "loadstep 28\n",
      "Newton iteration   0energy = 8.680177155339257<A u 0 , A u 0 >_{-1}^0.5 =  0.006045277540623081\n",
      "Newton iteration   1energy = 8.680159479753097<A u 1 , A u 1 >_{-1}^0.5 =  0.0007309703769520457\n",
      "Newton iteration   2energy = 8.680159212308286<A u 2 , A u 2 >_{-1}^0.5 =  5.488234785038055e-06\n",
      "Newton iteration   3energy = 8.68015921229323<A u 3 , A u 3 >_{-1}^0.5 =  1.2589353554913938e-09\n",
      "Newton iteration   4energy = 8.680159212293228<A u 4 , A u 4 >_{-1}^0.5 =  7.266489767493362e-15\n",
      "loadstep 29\n",
      "Newton iteration   0energy = 8.676609181924405<A u 0 , A u 0 >_{-1}^0.5 =  0.005883044873737748\n",
      "Newton iteration   1energy = 8.67659239742593<A u 1 , A u 1 >_{-1}^0.5 =  0.0006607926970899941\n",
      "Newton iteration   2energy = 8.676592178876996<A u 2 , A u 2 >_{-1}^0.5 =  4.539348109422138e-06\n",
      "Newton iteration   3energy = 8.676592178866697<A u 3 , A u 3 >_{-1}^0.5 =  8.403501989065408e-10\n",
      "Newton iteration   4energy =  8.6765921788667<A u 4 , A u 4 >_{-1}^0.5 =  7.67629239237462e-15\n",
      "loadstep 30\n",
      "Newton iteration   0energy = 8.673008438018613<A u 0 , A u 0 >_{-1}^0.5 =  0.005730281036566557\n",
      "Newton iteration   1energy = 8.672992476890737<A u 1 , A u 1 >_{-1}^0.5 =  0.0005990007596523957\n",
      "Newton iteration   2energy = 8.672992297311481<A u 2 , A u 2 >_{-1}^0.5 =  3.7670762457130904e-06\n",
      "Newton iteration   3energy = 8.672992297304384<A u 3 , A u 3 >_{-1}^0.5 =  5.648740373123977e-10\n",
      "Newton iteration   4energy = 8.672992297304384<A u 4 , A u 4 >_{-1}^0.5 =  8.035640983518844e-15\n",
      "loadstep 31\n",
      "Newton iteration   0energy = 8.669376546622743<A u 0 , A u 0 >_{-1}^0.5 =  0.005586260459714786\n",
      "Newton iteration   1energy = 8.669361347004394<A u 1 , A u 1 >_{-1}^0.5 =  0.0005444413363355358\n",
      "Newton iteration   2energy = 8.669361198654682<A u 2 , A u 2 >_{-1}^0.5 =  3.136626679136021e-06\n",
      "Newton iteration   3energy = 8.669361198649762<A u 3 , A u 3 >_{-1}^0.5 =  3.8234116538423575e-10\n",
      "Newton iteration   4energy = 8.66936119864977<A u 4 , A u 4 >_{-1}^0.5 =  8.562868014308272e-15\n",
      "loadstep 32\n",
      "Newton iteration   0energy = 8.665715002034561<A u 0 , A u 0 >_{-1}^0.5 =  0.005450322501437702\n",
      "Newton iteration   1energy = 8.665700507468294<A u 1 , A u 1 >_{-1}^0.5 =  0.0004961379326198621\n",
      "Newton iteration   2energy = 8.665700384279145<A u 2 , A u 2 >_{-1}^0.5 =  2.6203476609643125e-06\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Newton iteration   3energy = 8.66570038427571<A u 3 , A u 3 >_{-1}^0.5 =  2.6056777347312574e-10\n",
      "Newton iteration   4energy = 8.665700384275706<A u 4 , A u 4 >_{-1}^0.5 =  9.019629514694351e-15\n",
      "loadstep 33\n",
      "Newton iteration   0energy = 8.66202518273859<A u 0 , A u 0 >_{-1}^0.5 =  0.005321865247746466\n",
      "Newton iteration   1energy = 8.662011341718637<A u 1 , A u 1 >_{-1}^0.5 =  0.0004532612188666138\n",
      "Newton iteration   2energy = 8.662011238905643<A u 2 , A u 2 >_{-1}^0.5 =  2.1962280959714483e-06\n",
      "Newton iteration   3energy = 8.662011238903226<A u 3 , A u 3 >_{-1}^0.5 =  1.7877630682069847e-10\n",
      "Newton iteration   4energy = 8.662011238903228<A u 4 , A u 4 >_{-1}^0.5 =  7.564973133263078e-15\n",
      "loadstep 34\n",
      "Newton iteration   0energy = 8.658308362786434<A u 0 , A u 0 >_{-1}^0.5 =  0.005200339855866979\n",
      "Newton iteration   1energy = 8.658295128322484<A u 1 , A u 1 >_{-1}^0.5 =  0.000415104764483889\n",
      "Newton iteration   2energy = 8.658295042094178<A u 2 , A u 2 >_{-1}^0.5 =  1.8467124404900584e-06\n",
      "Newton iteration   3energy = 8.658295042092467<A u 3 , A u 3 >_{-1}^0.5 =  1.234695359286693e-10\n",
      "Newton iteration   4energy = 8.658295042092472<A u 4 , A u 4 >_{-1}^0.5 =  8.782606917524184e-15\n",
      "loadstep 35\n",
      "Newton iteration   0energy = 8.654565721865083<A u 0 , A u 0 >_{-1}^0.5 =  0.005085245422016683\n",
      "Newton iteration   1energy = 8.654553051070815<A u 1 , A u 1 >_{-1}^0.5 =  0.000381065074801646\n",
      "Newton iteration   2energy = 8.65455297840727<A u 2 , A u 2 >_{-1}^0.5 =  1.557767056817645e-06\n",
      "Newton iteration   3energy = 8.654552978406052<A u 3 , A u 3 >_{-1}^0.5 =  8.582529018395146e-11\n",
      "Newton iteration   4energy = 8.65455297840605<A u 4 , A u 4 >_{-1}^0.5 =  8.081681666189039e-15\n",
      "loadstep 36\n",
      "Newton iteration   0energy = 8.650798354223038<A u 0 , A u 0 >_{-1}^0.5 =  0.004976124343087951\n",
      "Newton iteration   1energy = 8.650786207933697<A u 1 , A u 1 >_{-1}^0.5 =  0.0003506251242975913\n",
      "Newton iteration   2energy = 8.650786146417616<A u 2 , A u 2 >_{-1}^0.5 =  1.3181457148090865e-06\n",
      "Newton iteration   3energy = 8.650786146416742<A u 3 , A u 3 >_{-1}^0.5 =  6.003412312235294e-11\n",
      "Newton iteration   4energy = 8.65078614641675<A u 4 , A u 4 >_{-1}^0.5 =  8.696956839627608e-15\n",
      "loadstep 37\n",
      "Newton iteration   0energy = 8.64700727659986<A u 0 , A u 0 >_{-1}^0.5 =  0.004872558136248435\n",
      "Newton iteration   1energy = 8.646995619020746<A u 1 , A u 1 >_{-1}^0.5 =  0.00032334073662541326\n",
      "Newton iteration   2energy = 8.646995566707893<A u 2 , A u 2 >_{-1}^0.5 =  1.1188120075819826e-06\n",
      "Newton iteration   3energy = 8.646995566707279<A u 3 , A u 3 >_{-1}^0.5 =  4.2253743915995646e-11\n",
      "Newton iteration   4energy = 8.646995566707277<A u 4 , A u 4 >_{-1}^0.5 =  9.377941245241211e-15\n",
      "loadstep 38\n",
      "Newton iteration   0energy = 8.64319343528497<A u 0 , A u 0 >_{-1}^0.5 =  0.0047741636782933534\n",
      "Newton iteration   1energy = 8.643182233670666<A u 1 , A u 1 >_{-1}^0.5 =  0.00029882928765195875\n",
      "Newton iteration   2energy = 8.643182188990053<A u 2 , A u 2 >_{-1}^0.5 =  9.524850091632305e-07\n",
      "Newton iteration   3energy = 8.643182188989602<A u 3 , A u 3 >_{-1}^0.5 =  2.991908303311475e-11\n",
      "Newton iteration   4energy = 8.643182188989597<A u 4 , A u 4 >_{-1}^0.5 =  8.794424361705271e-15\n",
      "loadstep 39\n",
      "Newton iteration   0energy = 8.639357712413794<A u 0 , A u 0 >_{-1}^0.5 =  0.004680589826624847\n",
      "Newton iteration   1energy = 8.639346936777114<A u 1 , A u 1 >_{-1}^0.5 =  0.0002767603087020712\n",
      "Newton iteration   2energy = 8.639346898453521<A u 2 , A u 2 >_{-1}^0.5 =  8.132815134318612e-07\n",
      "Newton iteration   3energy = 8.639346898453198<A u 3 , A u 3 >_{-1}^0.5 =  2.1307647885300333e-11\n",
      "Newton iteration   4energy = 8.639346898453194<A u 4 , A u 4 >_{-1}^0.5 =  8.826338258215783e-15\n",
      "loadstep 40\n",
      "Newton iteration   0energy = 8.63550093159491<A u 0 , A u 0 >_{-1}^0.5 =  0.004591514385093225\n",
      "Newton iteration   1energy = 8.635490554443683<A u 1 , A u 1 >_{-1}^0.5 =  0.0002568476480043424\n",
      "Newton iteration   2energy = 8.635490521437436<A u 2 , A u 2 >_{-1}^0.5 =  6.96433934687339e-07\n",
      "Newton iteration   3energy =  8.6354905214372<A u 3 , A u 3 >_{-1}^0.5 =  1.5261591030717674e-11\n",
      "Newton iteration   4energy = 8.63549052143719<A u 4 , A u 4 >_{-1}^0.5 =  9.275349131281183e-15\n",
      "loadstep 41\n",
      "Newton iteration   0energy = 8.631623862948826<A u 0 , A u 0 >_{-1}^0.5 =  0.004506641380400642\n",
      "Newton iteration   1energy = 8.631613859048633<A u 1 , A u 1 >_{-1}^0.5 =  0.00023884291352208444\n",
      "Newton iteration   2energy = 8.631613830508467<A u 2 , A u 2 >_{-1}^0.5 =  5.98067464130121e-07\n",
      "Newton iteration   3energy = 8.63161383050828<A u 3 , A u 3 >_{-1}^0.5 =  1.0991512102948347e-11\n",
      "Newton iteration   4energy = 8.631613830508293<A u 4 , A u 4 >_{-1}^0.5 =  9.388595441023754e-15\n",
      "loadstep 42\n",
      "Newton iteration   0energy = 8.627727227628869<A u 0 , A u 0 >_{-1}^0.5 =  0.0044256986169769995\n",
      "Newton iteration   1energy = 8.627717573789365<A u 1 , A u 1 >_{-1}^0.5 =  0.0002225299720352437\n",
      "Newton iteration   2energy = 8.627717549015356<A u 2 , A u 2 >_{-1}^0.5 =  5.150236660184008e-07\n",
      "Newton iteration   3energy = 8.62771754901523<A u 3 , A u 3 >_{-1}^0.5 =  7.961224220497201e-12\n",
      "Newton iteration   4energy = 8.62771754901523<A u 4 , A u 4 >_{-1}^0.5 =  9.341742069744362e-15\n",
      "loadstep 43\n",
      "Newton iteration   0energy = 8.623811701884499<A u 0 , A u 0 >_{-1}^0.5 =  0.004348435481444442\n",
      "Newton iteration   1energy = 8.62380237676742<A u 1 , A u 1 >_{-1}^0.5 =  0.00020772032165363919\n",
      "Newton iteration   2energy = 8.623802355181764<A u 2 , A u 2 >_{-1}^0.5 =  4.44720525750143e-07\n",
      "Newton iteration   3energy = 8.623802355181667<A u 3 , A u 3 >_{-1}^0.5 =  5.794433094929551e-12\n",
      "Newton iteration   4energy = 8.623802355181667<A u 4 , A u 4 >_{-1}^0.5 =  8.517398348223816e-15\n",
      "loadstep 44\n",
      "Newton iteration   0energy = 8.619877920720509<A u 0 , A u 0 >_{-1}^0.5 =  0.004274620970054603\n",
      "Newton iteration   1energy = 8.619868904667033<A u 1 , A u 1 >_{-1}^0.5 =  0.0001942491882238703\n",
      "Newton iteration   2energy = 8.619868885790854<A u 2 , A u 2 >_{-1}^0.5 =  3.850411392703588e-07\n",
      "Newton iteration   3energy = 8.619868885790778<A u 3 , A u 3 >_{-1}^0.5 =  4.240402111982851e-12\n",
      "Newton iteration   4energy = 8.619868885790774<A u 4 , A u 4 >_{-1}^0.5 =  8.326519656718127e-15\n",
      "loadstep 45\n",
      "Newton iteration   0energy = 8.61592648119783<A u 0 , A u 0 >_{-1}^0.5 =  0.004204041915484391\n",
      "Newton iteration   1energy = 8.615917756073554<A u 1 , A u 1 >_{-1}^0.5 =  0.0001819722236202162\n",
      "Newton iteration   2energy = 8.61591773950842<A u 2 , A u 2 >_{-1}^0.5 =  3.3424495536253416e-07\n",
      "Newton iteration   3energy = 8.615917739508363<A u 3 , A u 3 >_{-1}^0.5 =  3.1180198804711842e-12\n",
      "Newton iteration   4energy = 8.615917739508362<A u 4 , A u 4 >_{-1}^0.5 =  8.729211484647772e-15\n",
      "loadstep 46\n",
      "Newton iteration   0energy = 8.611957945416394<A u 0 , A u 0 >_{-1}^0.5 =  0.0041365013915693\n",
      "Newton iteration   1energy = 8.611949494472022<A u 1 , A u 1 >_{-1}^0.5 =  0.0001707627056471081\n",
      "Newton iteration   2energy = 8.611949479885219<A u 2 , A u 2 >_{-1}^0.5 =  2.908968256549781e-07\n",
      "Newton iteration   3energy = 8.611949479885176<A u 3 , A u 3 >_{-1}^0.5 =  2.3045214862315513e-12\n",
      "Newton iteration   4energy = 8.611949479885176<A u 4 , A u 4 >_{-1}^0.5 =  9.476505735026269e-15\n",
      "loadstep 47\n",
      "Newton iteration   0energy = 8.60797284321524<A u 0 , A u 0 >_{-1}^0.5 =  0.004071817276964948\n",
      "Newton iteration   1energy = 8.607964650961378<A u 1 , A u 1 >_{-1}^0.5 =  0.00016050915725754933\n",
      "Newton iteration   2energy = 8.60796463807402<A u 2 , A u 2 >_{-1}^0.5 =  2.5381012040075324e-07\n",
      "Newton iteration   3energy = 8.607964638073991<A u 3 , A u 3 >_{-1}^0.5 =  1.7112825412101932e-12\n",
      "Newton iteration   4energy = 8.60796463807399<A u 4 , A u 4 >_{-1}^0.5 =  9.433321793785772e-15\n",
      "loadstep 48\n",
      "Newton iteration   0energy = 8.60397167462065<A u 0 , A u 0 >_{-1}^0.5 =  0.004009820960714137\n",
      "Newton iteration   1energy = 8.603963726715204<A u 1 , A u 1 >_{-1}^0.5 =  0.0001511133171328678\n",
      "Newton iteration   2energy = 8.603963715292728<A u 2 , A u 2 >_{-1}^0.5 =  2.220010106422092e-07\n",
      "Newton iteration   3energy = 8.603963715292714<A u 3 , A u 3 >_{-1}^0.5 =  1.275556391486985e-12\n",
      "Newton iteration   4energy = 8.603963715292702<A u 4 , A u 4 >_{-1}^0.5 =  9.686107577932144e-15\n",
      "loadstep 49\n",
      "Newton iteration   0energy = 8.599954912069549<A u 0 , A u 0 >_{-1}^0.5 =  0.003950356174564131\n",
      "Newton iteration   1energy = 8.59994719521642<A u 1 , A u 1 >_{-1}^0.5 =  0.0001424884055043248\n",
      "Newton iteration   2energy = 8.599947185060834<A u 2 , A u 2 >_{-1}^0.5 =  1.9465161594403441e-07\n",
      "Newton iteration   3energy = 8.599947185060826<A u 3 , A u 3 >_{-1}^0.5 =  9.56721335441428e-13\n",
      "Newton iteration   4energy = 8.599947185060817<A u 4 , A u 4 >_{-1}^0.5 =  1.01442216725829e-14\n"
     ]
    }
   ],
   "source": [
    "gfu = GridFunction(V)\n",
    "gfu.vec[:] = 0\n",
    "\n",
    "Draw (gfu, mesh, \"u\")\n",
    "SetVisualization (deformation=True)\n",
    "\n",
    "res = gfu.vec.CreateVector()\n",
    "du = gfu.vec.CreateVector()\n",
    "\n",
    "for loadstep in range(50):\n",
    "    print (\"loadstep\", loadstep)\n",
    "    factor.Set ((loadstep+1)/10)\n",
    "    SolveNonlinearMinProblem(a,gfu)\n",
    "    Redraw()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Allen-Cahn equation\n",
    "\n",
    "The Allen-Cahn equations describe the process of phase separation and is the ($L^2$) gradient-flow equation to the energy\n",
    "$$\n",
    "  E(v) = \\int_{\\Omega} \\varepsilon \\vert \\nabla v \\vert^2~+~v^2(1-v^2) ~ dx\n",
    "$$\n",
    "\n",
    "i.e. the solution to the Allen-Cahn equation solves\n",
    "\n",
    "$$\n",
    "\\partial_t u = \\frac{\\delta E}{\\delta u}\n",
    "$$\n",
    "\n",
    "The quantity $u$ is an indicator for a phase where $-1$ refers to one phase and $1$ to another phase. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "The equation has two driving forces:\n",
    "\n",
    "- $u$ is pulled into one of the two minima ($-1$ and $1$) of the nonlinear term $u^2(1-u^2)$ (separation of the phases)\n",
    "- the diffusion term scaled with $\\varepsilon$ enforces a smooth transition between the two phases. $\\varepsilon$ determines the size of the transition layer\n",
    "\n",
    "We use the \"SymbolicEnergy\" feature to formulate the energy minimization problem and combine it with an implicit Euler discretization:\n",
    "\n",
    "$$\n",
    " M u^{n+1} - M u^n = \\Delta t \\underbrace{\\frac{\\delta E}{\\delta u}}_{=:A(u)} (u^{n+1})\n",
    "$$\n",
    "\n",
    "which we can interpreted as a nonlinear minimization problem again with the energy\n",
    "\n",
    "$$\n",
    "  E^{IE}(v) = \\int_{\\Omega} \\frac{\\varepsilon}{2} \\vert \\nabla v \\vert^2~+~v^2(1-v^2) + \\frac{1}{2\\Delta t} \\vert v - u^n \\vert^2 ~ dx\n",
    "$$\n",
    "\n",
    "To solve the nonlinear equation at every time step we again rely on Newton's method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "from ngsolve import *\n",
    "\n",
    "from netgen.geom2d import *\n",
    "\n",
    "\n",
    "periodic = SplineGeometry()\n",
    "pnts = [ (0,0), (1,0), (1,1), (0,1) ]\n",
    "pnums = [periodic.AppendPoint(*p) for p in pnts]\n",
    "\n",
    "lright = periodic.Append ( [\"line\", pnums[0], pnums[1]],bc=\"periodic\")\n",
    "btop = periodic.Append ( [\"line\", pnums[1], pnums[2]], bc=\"periodic\")\n",
    "periodic.Append ( [\"line\", pnums[3], pnums[2]], leftdomain=0, rightdomain=1, copy=lright, bc=\"periodic\")\n",
    "periodic.Append ( [\"line\", pnums[0], pnums[3]], leftdomain=0, rightdomain=1, copy=btop, bc=\"periodic\")\n",
    "\n",
    "mesh = Mesh (periodic.GenerateMesh(maxh=0.2))\n",
    "\n",
    "V = Periodic(H1(mesh, order=4, dirichlet=[]))\n",
    "\n",
    "u = V.TrialFunction()\n",
    "\n",
    "eps = 4e-3\n",
    "dt = 1e-1\n",
    "\n",
    "gfu = GridFunction(V)\n",
    "gfuold = GridFunction(V)\n",
    "\n",
    "a = BilinearForm (V, symmetric=False)\n",
    "a += SymbolicEnergy (eps/2*grad(u)*grad(u)\n",
    "                     + ((1-u*u)*(1-u*u))\n",
    "                     + 0.5/dt*(u-gfuold)*(u-gfuold))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "from math import pi\n",
    "gfu = GridFunction(V)\n",
    "gfu.Set(sin(2*pi*x))\n",
    "#gfu.Set(sin(1e8*x)) #<- essentially a random function\n",
    "Draw(gfu,mesh,\"u\")\n",
    "SetVisualization (deformation=False)\n",
    "t = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Newton iteration   0energy = 0.41450268691109665<A u 0 , A u 0 >_{-1}^0.5 =  0.2809232585283059\n",
      "Newton iteration   1energy = 0.37690029538736747<A u 1 , A u 1 >_{-1}^0.5 =  0.019176192659565692\n",
      "Newton iteration   2energy = 0.3767158234035433<A u 2 , A u 2 >_{-1}^0.5 =  9.630379284983538e-05\n",
      "Newton iteration   3energy = 0.37671581876625276<A u 3 , A u 3 >_{-1}^0.5 =  2.50838578139849e-09\n",
      "Newton iteration   4energy = 0.3767158187662527<A u 4 , A u 4 >_{-1}^0.5 =  2.9355370163738083e-16\n",
      "t =  0.1\n",
      "Newton iteration   0energy = 0.34388852354503213<A u 0 , A u 0 >_{-1}^0.5 =  0.23599828005106344\n",
      "Newton iteration   1energy = 0.3171273710024063<A u 1 , A u 1 >_{-1}^0.5 =  0.013452151221307806\n",
      "Newton iteration   2energy = 0.3170366836102077<A u 2 , A u 2 >_{-1}^0.5 =  4.664802779278174e-05\n",
      "Newton iteration   3energy = 0.3170366825221795<A u 3 , A u 3 >_{-1}^0.5 =  5.77460941464541e-10\n",
      "Newton iteration   4energy = 0.3170366825221793<A u 4 , A u 4 >_{-1}^0.5 =  3.163968816040173e-16\n",
      "t =  0.2\n",
      "Newton iteration   0energy = 0.2955290108422929<A u 0 , A u 0 >_{-1}^0.5 =  0.18333693294869569\n",
      "Newton iteration   1energy = 0.27922135353137706<A u 1 , A u 1 >_{-1}^0.5 =  0.008020819298689669\n",
      "Newton iteration   2energy = 0.2791891435391795<A u 2 , A u 2 >_{-1}^0.5 =  1.6287112762876847e-05\n",
      "Newton iteration   3energy = 0.2791891434065441<A u 3 , A u 3 >_{-1}^0.5 =  6.891275544015452e-11\n",
      "Newton iteration   4energy = 0.279189143406544<A u 4 , A u 4 >_{-1}^0.5 =  3.4938661083456126e-16\n",
      "t =  0.30000000000000004\n",
      "Newton iteration   0energy = 0.26701124384040154<A u 0 , A u 0 >_{-1}^0.5 =  0.1332937206028686\n",
      "Newton iteration   1energy = 0.25831515743654343<A u 1 , A u 1 >_{-1}^0.5 =  0.004187655354629702\n",
      "Newton iteration   2energy = 0.258306383137749<A u 2 , A u 2 >_{-1}^0.5 =  4.374151896800372e-06\n",
      "Newton iteration   3energy = 0.2583063831281824<A u 3 , A u 3 >_{-1}^0.5 =  4.879052593501367e-12\n",
      "Newton iteration   4energy = 0.2583063831281824<A u 4 , A u 4 >_{-1}^0.5 =  3.4662576970143404e-16\n",
      "t =  0.4\n",
      "Newton iteration   0energy = 0.25217846565739477<A u 0 , A u 0 >_{-1}^0.5 =  0.09212071263353547\n",
      "Newton iteration   1energy = 0.24799617525728274<A u 1 , A u 1 >_{-1}^0.5 =  0.001980203210485691\n",
      "Newton iteration   2energy = 0.24799421401922642<A u 2 , A u 2 >_{-1}^0.5 =  9.676166525426937e-07\n",
      "Newton iteration   3energy = 0.24799421401875826<A u 3 , A u 3 >_{-1}^0.5 =  2.352999798024643e-13\n",
      "Newton iteration   4energy = 0.24799421401875832<A u 4 , A u 4 >_{-1}^0.5 =  3.6774826879915607e-16\n",
      "t =  0.5\n",
      "Newton iteration   0energy = 0.24516863305276446<A u 0 , A u 0 >_{-1}^0.5 =  0.06141505016588461\n",
      "Newton iteration   1energy = 0.24330052673100364<A u 1 , A u 1 >_{-1}^0.5 =  0.0008740389330635108\n",
      "Newton iteration   2energy = 0.24330014470463096<A u 2 , A u 2 >_{-1}^0.5 =  1.8731959108252394e-07\n",
      "Newton iteration   3energy = 0.24330014470461345<A u 3 , A u 3 >_{-1}^0.5 =  8.746323616126442e-15\n",
      "t =  0.6\n",
      "Newton iteration   0energy = 0.24207365834808142<A u 0 , A u 0 >_{-1}^0.5 =  0.03997539205980796\n",
      "Newton iteration   1energy = 0.2412795131472567<A u 1 , A u 1 >_{-1}^0.5 =  0.0003689785123362593\n",
      "Newton iteration   2energy = 0.24127944507060162<A u 2 , A u 2 >_{-1}^0.5 =  3.3363958742417324e-08\n",
      "Newton iteration   3energy = 0.24127944507060106<A u 3 , A u 3 >_{-1}^0.5 =  4.786315775211166e-16\n",
      "t =  0.7\n",
      "Newton iteration   0energy = 0.24076734060641394<A u 0 , A u 0 >_{-1}^0.5 =  0.02564726714970522\n",
      "Newton iteration   1energy = 0.24043973271028393<A u 1 , A u 1 >_{-1}^0.5 =  0.0001520242371548029\n",
      "Newton iteration   2energy = 0.24043972115431173<A u 2 , A u 2 >_{-1}^0.5 =  5.730888743528835e-09\n",
      "Newton iteration   3energy = 0.24043972115431161<A u 3 , A u 3 >_{-1}^0.5 =  3.9497354526053974e-16\n",
      "t =  0.7999999999999999\n",
      "Newton iteration   0energy = 0.2402303872880181<A u 0 , A u 0 >_{-1}^0.5 =  0.016353675259529705\n",
      "Newton iteration   1energy = 0.24009700062875125<A u 1 , A u 1 >_{-1}^0.5 =  6.246094502087166e-05\n",
      "Newton iteration   2energy = 0.24009699867804568<A u 2 , A u 2 >_{-1}^0.5 =  1.0198825510076212e-09\n",
      "Newton iteration   3energy = 0.24009699867804568<A u 3 , A u 3 >_{-1}^0.5 =  3.910355678385764e-16\n",
      "t =  0.8999999999999999\n",
      "Newton iteration   0energy = 0.24001177430570772<A u 0 , A u 0 >_{-1}^0.5 =  0.01046198510439957\n",
      "Newton iteration   1energy = 0.23995713746382227<A u 1 , A u 1 >_{-1}^0.5 =  2.6467926688190138e-05\n",
      "Newton iteration   2energy = 0.23995713711354497<A u 2 , A u 2 >_{-1}^0.5 =  2.2098631704426873e-10\n",
      "Newton iteration   3energy = 0.23995713711354494<A u 3 , A u 3 >_{-1}^0.5 =  4.2671806155941764e-16\n",
      "t =  0.9999999999999999\n",
      "Newton iteration   0energy = 0.2399217819098009<A u 0 , A u 0 >_{-1}^0.5 =  0.006812071313422244\n",
      "Newton iteration   1energy = 0.2398986060461037<A u 1 , A u 1 >_{-1}^0.5 =  1.230212547904795e-05\n",
      "Newton iteration   2energy = 0.2398986059704322<A u 2 , A u 2 >_{-1}^0.5 =  7.099126365185817e-11\n",
      "Newton iteration   3energy = 0.23989860597043222<A u 3 , A u 3 >_{-1}^0.5 =  3.8095111854675177e-16\n",
      "t =  1.0999999999999999\n",
      "Newton iteration   0energy = 0.23988309433332944<A u 0 , A u 0 >_{-1}^0.5 =  0.004621770715225076\n",
      "Newton iteration   1energy = 0.23987242312588983<A u 1 , A u 1 >_{-1}^0.5 =  6.7942416189496265e-06\n",
      "Newton iteration   2energy = 0.23987242310280898<A u 2 , A u 2 >_{-1}^0.5 =  3.209524829098261e-11\n",
      "Newton iteration   3energy = 0.23987242310280898<A u 3 , A u 3 >_{-1}^0.5 =  4.2018315330435895e-16\n",
      "t =  1.2\n",
      "Newton iteration   0energy = 0.23986480836454233<A u 0 , A u 0 >_{-1}^0.5 =  0.003368954272575993\n",
      "Newton iteration   1energy = 0.23985913748728932<A u 1 , A u 1 >_{-1}^0.5 =  4.576099681476189e-06\n",
      "Newton iteration   2energy = 0.2398591374768189<A u 2 , A u 2 >_{-1}^0.5 =  1.7623725590715833e-11\n",
      "Newton iteration   3energy = 0.23985913747681892<A u 3 , A u 3 >_{-1}^0.5 =  4.270044197551166e-16\n",
      "t =  1.3\n",
      "Newton iteration   0energy = 0.23985470889450997<A u 0 , A u 0 >_{-1}^0.5 =  0.002692728902482192\n",
      "Newton iteration   1energy = 0.23985108571269736<A u 1 , A u 1 >_{-1}^0.5 =  3.5610540851343554e-06\n",
      "Newton iteration   2energy = 0.2398510857063568<A u 2 , A u 2 >_{-1}^0.5 =  1.0946944604129284e-11\n",
      "Newton iteration   3energy = 0.23985108570635671<A u 3 , A u 3 >_{-1}^0.5 =  4.396452732575224e-16\n",
      "t =  1.4000000000000001\n",
      "Newton iteration   0energy = 0.23984799171546298<A u 0 , A u 0 >_{-1}^0.5 =  0.0023401679038810375\n",
      "Newton iteration   1energy = 0.2398452549179071<A u 1 , A u 1 >_{-1}^0.5 =  3.005473339170729e-06\n",
      "Newton iteration   2energy = 0.23984525491339054<A u 2 , A u 2 >_{-1}^0.5 =  7.442673310088071e-12\n",
      "Newton iteration   3energy = 0.23984525491339057<A u 3 , A u 3 >_{-1}^0.5 =  4.491259343620475e-16\n",
      "t =  1.5000000000000002\n",
      "Newton iteration   0energy = 0.23984276557831818<A u 0 , A u 0 >_{-1}^0.5 =  0.0021500366176415617\n",
      "Newton iteration   1energy = 0.23984045520885888<A u 1 , A u 1 >_{-1}^0.5 =  2.6479579130120444e-06\n",
      "Newton iteration   2energy = 0.23984045520535296<A u 2 , A u 2 >_{-1}^0.5 =  5.422860528597392e-12\n",
      "Newton iteration   3energy = 0.23984045520535302<A u 3 , A u 3 >_{-1}^0.5 =  4.1474306886148735e-16\n",
      "t =  1.6000000000000003\n",
      "Newton iteration   0energy = 0.23983827920581985<A u 0 , A u 0 >_{-1}^0.5 =  0.0020348851953489473\n",
      "Newton iteration   1energy = 0.2398362095246352<A u 1 , A u 1 >_{-1}^0.5 =  2.3877185904425255e-06\n",
      "Newton iteration   2energy = 0.2398362095217846<A u 2 , A u 2 >_{-1}^0.5 =  4.161521766436116e-12\n",
      "Newton iteration   3energy = 0.2398362095217846<A u 3 , A u 3 >_{-1}^0.5 =  4.120990651108714e-16\n",
      "t =  1.7000000000000004\n",
      "Newton iteration   0energy = 0.2398342271470195<A u 0 , A u 0 >_{-1}^0.5 =  0.001953342985032429\n",
      "Newton iteration   1energy = 0.23983231990182843<A u 1 , A u 1 >_{-1}^0.5 =  2.181270857293332e-06\n",
      "Newton iteration   2energy = 0.23983231989944948<A u 2 , A u 2 >_{-1}^0.5 =  3.3146217660229777e-12\n",
      "Newton iteration   3energy = 0.23983231989944945<A u 3 , A u 3 >_{-1}^0.5 =  4.3013449935758775e-16\n",
      "t =  1.8000000000000005\n",
      "Newton iteration   0energy = 0.23983047892544848<A u 0 , A u 0 >_{-1}^0.5 =  0.0018873221834727024\n",
      "Newton iteration   1energy = 0.23982869834814605<A u 1 , A u 1 >_{-1}^0.5 =  2.0081124626660635e-06\n",
      "Newton iteration   2energy = 0.23982869834612983<A u 2 , A u 2 >_{-1}^0.5 =  2.7089926940188982e-12\n",
      "Newton iteration   3energy = 0.2398286983461298<A u 3 , A u 3 >_{-1}^0.5 =  4.136934679341362e-16\n",
      "t =  1.9000000000000006\n",
      "Newton iteration   0energy = 0.23982697342606712<A u 0 , A u 0 >_{-1}^0.5 =  0.0018291572337895208\n",
      "Newton iteration   1energy = 0.23982530085281606<A u 1 , A u 1 >_{-1}^0.5 =  1.8577922988903242e-06\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Newton iteration   2energy = 0.23982530085109044<A u 2 , A u 2 >_{-1}^0.5 =  2.253601788515063e-12\n",
      "Newton iteration   3energy = 0.2398253008510904<A u 3 , A u 3 >_{-1}^0.5 =  4.161732600430683e-16\n",
      "t =  2.0000000000000004\n",
      "Newton iteration   0energy = 0.23982367762908566<A u 0 , A u 0 >_{-1}^0.5 =  0.0017755912541635457\n",
      "Newton iteration   1energy = 0.23982210154298106<A u 1 , A u 1 >_{-1}^0.5 =  1.7245245368461245e-06\n",
      "Newton iteration   2energy = 0.23982210154149403<A u 2 , A u 2 >_{-1}^0.5 =  1.8979705571118188e-12\n",
      "Newton iteration   3energy = 0.23982210154149403<A u 3 , A u 3 >_{-1}^0.5 =  3.9958496757931224e-16\n",
      "t =  2.1000000000000005\n",
      "Newton iteration   0energy = 0.2398205703929944<A u 0 , A u 0 >_{-1}^0.5 =  0.0017251940955923843\n",
      "Newton iteration   1energy = 0.23981908247720582<A u 1 , A u 1 >_{-1}^0.5 =  1.6048109259649143e-06\n",
      "Newton iteration   2energy = 0.23981908247591813<A u 2 , A u 2 >_{-1}^0.5 =  1.6126836894271255e-12\n",
      "Newton iteration   3energy = 0.2398190824759181<A u 3 , A u 3 >_{-1}^0.5 =  3.66015241147275e-16\n",
      "t =  2.2000000000000006\n",
      "Newton iteration   0energy = 0.2398176360054435<A u 0 , A u 0 >_{-1}^0.5 =  0.0016772924085901265\n",
      "Newton iteration   1energy = 0.2398162295473618<A u 1 , A u 1 >_{-1}^0.5 =  1.4963465114958576e-06\n",
      "Newton iteration   2energy = 0.2398162295462423<A u 2 , A u 2 >_{-1}^0.5 =  1.3792864549581004e-12\n",
      "Newton iteration   3energy = 0.23981622954624232<A u 3 , A u 3 >_{-1}^0.5 =  4.0724372300964665e-16\n",
      "t =  2.3000000000000007\n",
      "Newton iteration   0energy = 0.23981486155942658<A u 0 , A u 0 >_{-1}^0.5 =  0.0016315314196973449\n",
      "Newton iteration   1energy = 0.23981353078108197<A u 1 , A u 1 >_{-1}^0.5 =  1.3974937765274695e-06\n",
      "Newton iteration   2energy = 0.23981353078010562<A u 2 , A u 2 >_{-1}^0.5 =  1.185661712735239e-12\n",
      "Newton iteration   3energy = 0.2398135307801056<A u 3 , A u 3 >_{-1}^0.5 =  4.0692764831583306e-16\n",
      "t =  2.400000000000001\n",
      "Newton iteration   0energy = 0.23981223583993277<A u 0 , A u 0 >_{-1}^0.5 =  0.001587695231112611\n",
      "Newton iteration   1energy = 0.23981097559818157<A u 1 , A u 1 >_{-1}^0.5 =  1.307016701083898e-06\n",
      "Newton iteration   2energy = 0.23981097559732756<A u 2 , A u 2 >_{-1}^0.5 =  1.0234087151902238e-12\n",
      "Newton iteration   3energy = 0.23981097559732759<A u 3 , A u 3 >_{-1}^0.5 =  4.2109059650142165e-16\n",
      "t =  2.500000000000001\n",
      "Newton iteration   0energy = 0.23980974881596231<A u 0 , A u 0 >_{-1}^0.5 =  0.001545632320221452\n",
      "Newton iteration   1energy = 0.23980855445375673<A u 1 , A u 1 >_{-1}^0.5 =  1.2239382146600934e-06\n",
      "Newton iteration   2energy = 0.23980855445300783<A u 2 , A u 2 >_{-1}^0.5 =  8.864704675716774e-13\n",
      "Newton iteration   3energy = 0.2398085544530078<A u 3 , A u 3 >_{-1}^0.5 =  3.860503999338409e-16\n",
      "t =  2.600000000000001\n",
      "Newton iteration   0energy = 0.23980739138160234<A u 0 , A u 0 >_{-1}^0.5 =  0.0015052239652246484\n",
      "Newton iteration   1energy = 0.2398062586435172<A u 1 , A u 1 >_{-1}^0.5 =  1.1474589658054772e-06\n",
      "Newton iteration   2energy = 0.23980625864285895<A u 2 , A u 2 >_{-1}^0.5 =  7.700762845886604e-13\n",
      "Newton iteration   3energy = 0.23980625864285893<A u 3 , A u 3 >_{-1}^0.5 =  4.3557538295205123e-16\n",
      "t =  2.700000000000001\n",
      "Newton iteration   0energy = 0.23980515520427065<A u 0 , A u 0 >_{-1}^0.5 =  0.0014663703545176434\n",
      "Newton iteration   1energy = 0.23980408018121405<A u 1 , A u 1 >_{-1}^0.5 =  1.0769081798492949e-06\n",
      "Newton iteration   2energy = 0.23980408018063423<A u 2 , A u 2 >_{-1}^0.5 =  6.70806280629591e-13\n",
      "Newton iteration   3energy = 0.23980408018063418<A u 3 , A u 3 >_{-1}^0.5 =  4.1211995666328175e-16\n",
      "t =  2.800000000000001\n",
      "Newton iteration   0energy = 0.2398030326225266<A u 0 , A u 0 >_{-1}^0.5 =  0.0014289841085793756\n",
      "Newton iteration   1energy = 0.23980201171105245<A u 1 , A u 1 >_{-1}^0.5 =  1.0117122443812797e-06\n",
      "Newton iteration   2energy = 0.2398020117105407<A u 2 , A u 2 >_{-1}^0.5 =  5.857209839429618e-13\n",
      "Newton iteration   3energy = 0.23980201171054066<A u 3 , A u 3 >_{-1}^0.5 =  4.136326079471177e-16\n",
      "t =  2.9000000000000012\n",
      "Newton iteration   0energy = 0.23980101656930852<A u 0 , A u 0 >_{-1}^0.5 =  0.0013929869965262228\n",
      "Newton iteration   1energy = 0.23980004643919056<A u 1 , A u 1 >_{-1}^0.5 =  9.513736147237864e-07\n",
      "Newton iteration   2energy = 0.23980004643873795<A u 2 , A u 2 >_{-1}^0.5 =  5.12637238731772e-13\n",
      "Newton iteration   3energy = 0.23980004643873803<A u 3 , A u 3 >_{-1}^0.5 =  4.0818478086929167e-16\n",
      "t =  3.0000000000000013\n",
      "Newton iteration   0energy = 0.23979910050996395<A u 0 , A u 0 >_{-1}^0.5 =  0.0013583080881265145\n",
      "Newton iteration   1energy = 0.23979817807707918<A u 1 , A u 1 >_{-1}^0.5 =  8.954560268805773e-07\n",
      "Newton iteration   2energy = 0.23979817807667816<A u 2 , A u 2 >_{-1}^0.5 =  4.496599978112209e-13\n",
      "Newton iteration   3energy = 0.2397981780766781<A u 3 , A u 3 >_{-1}^0.5 =  3.7320188102935894e-16\n",
      "t =  3.1000000000000014\n",
      "Newton iteration   0energy = 0.23979727839003487<A u 0 , A u 0 >_{-1}^0.5 =  0.001324882589652414\n",
      "Newton iteration   1energy = 0.23979640079304174<A u 1 , A u 1 >_{-1}^0.5 =  8.435737332075263e-07\n",
      "Newton iteration   2energy = 0.23979640079268588<A u 2 , A u 2 >_{-1}^0.5 =  3.9516854540745673e-13\n",
      "Newton iteration   3energy = 0.23979640079268588<A u 3 , A u 3 >_{-1}^0.5 =  3.7393791096780473e-16\n",
      "t =  3.2000000000000015\n",
      "Newton iteration   0energy = 0.23979554459015517<A u 0 , A u 0 >_{-1}^0.5 =  0.0012926510309648675\n",
      "Newton iteration   1energy = 0.23979470917010964<A u 1 , A u 1 >_{-1}^0.5 =  7.953834056061581e-07\n",
      "Newton iteration   2energy = 0.23979470916979337<A u 2 , A u 2 >_{-1}^0.5 =  3.480245574719867e-13\n",
      "Newton iteration   3energy = 0.23979470916979337<A u 3 , A u 3 >_{-1}^0.5 =  3.872132135793069e-16\n",
      "t =  3.3000000000000016\n",
      "Newton iteration   0energy = 0.2397938938865219<A u 0 , A u 0 >_{-1}^0.5 =  0.001261558649417167\n",
      "Newton iteration   1energy = 0.2397930981688792<A u 1 , A u 1 >_{-1}^0.5 =  7.505778682442833e-07\n",
      "Newton iteration   2energy = 0.23979309816859737<A u 2 , A u 2 >_{-1}^0.5 =  3.070918168492527e-13\n",
      "Newton iteration   3energy = 0.2397930981685974<A u 3 , A u 3 >_{-1}^0.5 =  3.7432778189623423e-16\n",
      "t =  3.4000000000000017\n",
      "Newton iteration   0energy = 0.2397923214159341<A u 0 , A u 0 >_{-1}^0.5 =  0.001231554894418221\n",
      "Newton iteration   1energy = 0.23979156309455196<A u 1 , A u 1 >_{-1}^0.5 =  7.088811244437789e-07\n",
      "Newton iteration   2energy = 0.23979156309430072<A u 2 , A u 2 >_{-1}^0.5 =  2.7146435119184975e-13\n",
      "Newton iteration   3energy = 0.2397915630943007<A u 3 , A u 3 >_{-1}^0.5 =  4.055972384702122e-16\n",
      "t =  3.5000000000000018\n",
      "Newton iteration   0energy = 0.239790822644684<A u 0 , A u 0 >_{-1}^0.5 =  0.0012025930123929097\n",
      "Newton iteration   1energy = 0.2397900995675406<A u 1 , A u 1 >_{-1}^0.5 =  6.700443260192641e-07\n",
      "Newton iteration   2energy = 0.23979009956731617<A u 2 , A u 2 >_{-1}^0.5 =  2.4039198923922215e-13\n",
      "Newton iteration   3energy = 0.2397900995673162<A u 3 , A u 3 >_{-1}^0.5 =  4.1530851920836197e-16\n",
      "t =  3.600000000000002\n",
      "Newton iteration   0energy = 0.2397893933407546<A u 0 , A u 0 >_{-1}^0.5 =  0.0011746296892646987\n",
      "Newton iteration   1energy = 0.2397887034971503<A u 1 , A u 1 >_{-1}^0.5 =  6.338424467591721e-07\n",
      "Newton iteration   2energy = 0.23978870349694945<A u 2 , A u 2 >_{-1}^0.5 =  2.1332234861683952e-13\n",
      "Newton iteration   3energy = 0.23978870349694942<A u 3 , A u 3 >_{-1}^0.5 =  3.994075992214168e-16\n",
      "t =  3.700000000000002\n",
      "Newton iteration   0energy = 0.23978802954889125<A u 0 , A u 0 >_{-1}^0.5 =  0.0011476247365417888\n",
      "Newton iteration   1energy = 0.23978737105794926<A u 1 , A u 1 >_{-1}^0.5 =  6.00071494532039e-07\n",
      "Newton iteration   2energy = 0.2397873710577693<A u 2 , A u 2 >_{-1}^0.5 =  1.8963750662706926e-13\n",
      "Newton iteration   3energy = 0.23978737105776934<A u 3 , A u 3 >_{-1}^0.5 =  3.9508651477264857e-16\n",
      "t =  3.800000000000002\n",
      "Newton iteration   0energy = 0.2397867275681889<A u 0 , A u 0 >_{-1}^0.5 =  0.001121540812005155\n",
      "Newton iteration   1energy = 0.23978609866849762<A u 1 , A u 1 >_{-1}^0.5 =  5.685461452199974e-07\n",
      "Newton iteration   2energy = 0.239786098668336<A u 2 , A u 2 >_{-1}^0.5 =  1.6881169362487337e-13\n",
      "Newton iteration   3energy = 0.23978609866833606<A u 3 , A u 3 >_{-1}^0.5 =  3.7362237903421027e-16\n",
      "t =  3.900000000000002\n",
      "Newton iteration   0energy = 0.23978548393189394<A u 0 , A u 0 >_{-1}^0.5 =  0.001096343168856977\n",
      "Newton iteration   1energy = 0.23978488297215855<A u 1 , A u 1 >_{-1}^0.5 =  5.390977122982297e-07\n",
      "Newton iteration   2energy = 0.2397848829720133<A u 2 , A u 2 >_{-1}^0.5 =  1.506206667808283e-13\n",
      "Newton iteration   3energy = 0.23978488297201328<A u 3 , A u 3 >_{-1}^0.5 =  4.0256707927596727e-16\n",
      "t =  4.000000000000002\n",
      "Newton iteration   0energy = 0.2397842953891668<A u 0 , A u 0 >_{-1}^0.5 =  0.001071999428939726\n",
      "Newton iteration   1energy = 0.2397837208197555<A u 1 , A u 1 >_{-1}^0.5 =  5.115723902938338e-07\n",
      "Newton iteration   2energy = 0.23978372081962457<A u 2 , A u 2 >_{-1}^0.5 =  1.3461240330667862e-13\n",
      "Newton iteration   3energy = 0.23978372081962462<A u 3 , A u 3 >_{-1}^0.5 =  4.22810320364615e-16\n",
      "t =  4.100000000000001\n",
      "Newton iteration   0energy = 0.23978315888858148<A u 0 , A u 0 >_{-1}^0.5 =  0.0010484793767550234\n",
      "Newton iteration   1energy = 0.23978260925386716<A u 1 , A u 1 >_{-1}^0.5 =  4.858297225032861e-07\n",
      "Newton iteration   2energy = 0.23978260925374922<A u 2 , A u 2 >_{-1}^0.5 =  1.2049305730754915e-13\n",
      "Newton iteration   3energy = 0.23978260925374922<A u 3 , A u 3 >_{-1}^0.5 =  4.2049767882605063e-16\n",
      "t =  4.200000000000001\n",
      "Newton iteration   0energy = 0.23978207156317277<A u 0 , A u 0 >_{-1}^0.5 =  0.0010257547717514503\n",
      "Newton iteration   1energy = 0.23978154549458386<A u 1 , A u 1 >_{-1}^0.5 =  4.6174125702286747e-07\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Newton iteration   2energy = 0.23978154549447736<A u 2 , A u 2 >_{-1}^0.5 =  1.0804519197732318e-13\n",
      "Newton iteration   3energy = 0.23978154549447733<A u 3 , A u 3 >_{-1}^0.5 =  3.75485072305106e-16\n",
      "t =  4.300000000000001\n",
      "Newton iteration   0energy = 0.23978103071686038<A u 0 , A u 0 >_{-1}^0.5 =  0.0010037991768567201\n",
      "Newton iteration   1energy = 0.23978052692656368<A u 1 , A u 1 >_{-1}^0.5 =  4.3918936193603866e-07\n",
      "Newton iteration   2energy = 0.2397805269264672<A u 2 , A u 2 >_{-1}^0.5 =  9.708485837881644e-14\n",
      "t =  4.4\n",
      "Newton iteration   0energy = 0.23978003381210533<A u 0 , A u 0 >_{-1}^0.5 =  0.0009825878015857251\n",
      "Newton iteration   1energy = 0.23977955108725418<A u 1 , A u 1 >_{-1}^0.5 =  4.180661760124394e-07\n",
      "Newton iteration   2energy = 0.23977955108716673<A u 2 , A u 2 >_{-1}^0.5 =  8.732008667811928e-14\n",
      "t =  4.5\n",
      "Newton iteration   0energy = 0.23977907845866553<A u 0 , A u 0 >_{-1}^0.5 =  0.0009620973583148351\n",
      "Newton iteration   1energy = 0.2397786156561545<A u 1 , A u 1 >_{-1}^0.5 =  3.9827267609246794e-07\n",
      "Newton iteration   2energy = 0.23977861565607522<A u 2 , A u 2 >_{-1}^0.5 =  7.871313441500204e-14\n",
      "t =  4.6\n",
      "Newton iteration   0energy = 0.23977816240333752<A u 0 , A u 0 >_{-1}^0.5 =  0.0009423059305070753\n",
      "Newton iteration   1energy = 0.23977771844501206<A u 1 , A u 1 >_{-1}^0.5 =  3.797178460263959e-07\n",
      "Newton iteration   2energy = 0.2397777184449398<A u 2 , A u 2 >_{-1}^0.5 =  7.107647461255042e-14\n",
      "t =  4.699999999999999\n",
      "Newton iteration   0energy = 0.23977728352058292<A u 0 , A u 0 >_{-1}^0.5 =  0.0009231928518233563\n",
      "Newton iteration   1energy = 0.23977685738885424<A u 1 , A u 1 >_{-1}^0.5 =  3.6231793361432817e-07\n",
      "Newton iteration   2energy = 0.23977685738878857<A u 2 , A u 2 >_{-1}^0.5 =  6.426868881058818e-14\n",
      "t =  4.799999999999999\n",
      "Newton iteration   0energy = 0.2397764398039481<A u 0 , A u 0 >_{-1}^0.5 =  0.0009047385951755221\n",
      "Newton iteration   1energy = 0.23977603053777619<A u 1 , A u 1 >_{-1}^0.5 =  3.459957855427896e-07\n",
      "Newton iteration   2energy = 0.23977603053771646<A u 2 , A u 2 >_{-1}^0.5 =  5.827355843816445e-14\n",
      "t =  4.899999999999999\n",
      "Newton iteration   0energy = 0.23977562935819674<A u 0 , A u 0 >_{-1}^0.5 =  0.0008869246708769174\n",
      "Newton iteration   1energy = 0.23977523604940246<A u 1 , A u 1 >_{-1}^0.5 =  3.306802497027191e-07\n",
      "Newton iteration   2energy = 0.2397752360493478<A u 2 , A u 2 >_{-1}^0.5 =  5.288358902541465e-14\n",
      "t =  4.999999999999998\n"
     ]
    }
   ],
   "source": [
    "for timestep in range(50):\n",
    "    gfuold.vec.data = gfu.vec\n",
    "    SolveNonlinearMinProblem(a,gfu)\n",
    "    Redraw() \n",
    "    t += dt\n",
    "    print(\"t = \", t)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
